package com.cskaoyan.app.wxcontroller;

import com.cskaoyan.app.bean.bo.CartCheckedBo;
import com.cskaoyan.app.bean.bo.CartCheckoutBo;
import com.cskaoyan.app.bean.bo.CartCheckoutRequestBo;
import com.cskaoyan.app.bean.common.BaseRespVo;
import com.cskaoyan.app.bean.vo.CartDataVo;
import com.cskaoyan.app.wxservice.WxCartService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author: 廖平安
 * @CreateTime: 2022-12-31  15:17
 * @Description: TODO
 * @Version: 1.0
 */
@RequestMapping("wx/cart")
@RestController
public class WxCartController {
    @Autowired
    private WxCartService cartService;

    @PostMapping("add")
    public BaseRespVo add(@RequestBody Map map) {
        Subject subject = SecurityUtils.getSubject();
        Integer userId = (Integer) subject.getSession().getAttribute("userid");
        if (userId != null) {
            Integer data = cartService.add(map, userId);
            return BaseRespVo.ok(data);
        }
        return new BaseRespVo(501, "请登录", null);
    }

    @GetMapping("index")
    public BaseRespVo index() {
        CartDataVo data = cartService.index();
        return BaseRespVo.ok(data);
    }

    @PostMapping("update")
    public BaseRespVo update(@RequestBody Map map) {
        cartService.update(map);
        return BaseRespVo.ok();
    }

    @PostMapping("checked")
    public BaseRespVo checked(@RequestBody CartCheckedBo cartCheckedBo) {
        CartDataVo data = cartService.checked(cartCheckedBo);
        return BaseRespVo.ok(data);
    }

    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody CartCheckedBo cartCheckedBo) {
        CartDataVo data = cartService.delete(cartCheckedBo);
        return BaseRespVo.ok(data);
    }

    @GetMapping("goodscount")
    public BaseRespVo goodsCount() {
        Integer data = cartService.goodsCount();
        if(data==null){
            data=0;
        }
        return BaseRespVo.ok(data);
    }
    @PostMapping("fastadd")
    public BaseRespVo fastAdd(@RequestBody Map map){
        Subject subject = SecurityUtils.getSubject();
        Integer userId = (Integer) subject.getSession().getAttribute("userid");
        if (userId != null) {
            Integer data = cartService.fastAdd(map, userId);
            return BaseRespVo.ok(data);
        }
        return new BaseRespVo(501, "请登录", null);
    }
    @GetMapping("checkout")
    public BaseRespVo checkout( CartCheckoutRequestBo checkoutRequestBo) {
        CartCheckoutBo data = cartService.checkout(checkoutRequestBo);
        return BaseRespVo.ok(data);
    }
}
